<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\annotation\route\Get;
use think\annotation\route\Group;
use app\common\model\Qrcode;
use think\annotation\route\Post;

#[Group("user")]
class User extends Api
{
    #[Get('userinfo')]
    public function userinfo()
    {
        $user=$this->auth->userinfo();
        $this->success('',$user);
    }

    #[Get('test')]
    public function test()
    {
        $this->success('你已经登陆过了');
    }

    #[Post('qrcode')]
    public function qrcode()
    {
        $type=$this->request->post('type');
        $foreign_key=$this->request->post('foreign_key');
        $qrcode=Qrcode::where(['type'=>$type,'foreign_key'=>$foreign_key])->find();
        if(!$qrcode){
            $qrcode=new Qrcode();
            $qrcode->type=$type;
            $qrcode->foreign_key=$foreign_key;
            $qrcode->save();
        }
        $config=[
            'appid'=>site_config("uniapp.mpapp_id"),
            'appsecret'=>site_config("uniapp.mpapp_secret"),
        ];
        $wechat=new \WeChat\Qrcode($config);
        $ticket = $wechat->create($qrcode->id)['ticket'];
        $url=$wechat->url($ticket);
        $this->success('',$url);
    }
}
